package com.justRun;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.widget.Toast;

public class FileManager {
	private String fileName;
	private String fileTemp = "temp.txt";
	private Context leContext;
	private int n = 1;
	private FileOutputStream fileOut = null; 
	private OutputStreamWriter writer = null;
	private FileInputStream fileIn = null; 
	private InputStreamReader inputReader = null; 
	private BufferedReader reader = null;
	private boolean writing = false;
	private boolean reading = false;

	public FileManager(Context c, String nomFic) {
		leContext = c;
		this.fileName = nomFic;
	}

	public void reset() {
		n = 1;
	}

	public String readLine() throws FileNotFoundException, IOException {
		if(reading) {
			return reader.readLine();
		} else {
			try {
				return readLine(n++);
			} catch (FileNotFoundException e) {
				throw e;
			} catch (IOException e) {
				throw e;
			}
		}
	}

	public void printPath() {
		System.out.println(leContext.getFileStreamPath(fileName).getAbsolutePath());		
	}
	
	public boolean fileExists() {
		FileInputStream fIn = null; 
		try {
			fIn = leContext.openFileInput(fileName);
			fIn.close();
			return true;
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return true;
		}
	}
	
	public void skipLine(int n) {
		if (reading) {
			int i = 0;
			try {
				while(i < n) {
					reader.readLine();
					i++;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public int getNbLines() {
		FileInputStream fIn = null; 
		InputStreamReader isr = null; 
		BufferedReader bufRead = null;
		int res = 0;

		try {
			fIn = leContext.openFileInput(fileName);
		} catch (FileNotFoundException e1) {
			return res;
		}       
		try {
			isr = new InputStreamReader(fIn); 
			bufRead = new BufferedReader(isr);
			while(bufRead.readLine() != null) {
				res++;
			}	
			bufRead.close();
			isr.close();
			fIn.close();
			return res;
		} catch (IOException e) {
			return res;
		}
	}

	public String readLine(int numLine) throws FileNotFoundException, IOException {
		if (reading) {
			stopReading();
			reading = true;
		}
		FileInputStream fIn = null; 
		InputStreamReader isr = null; 
		BufferedReader bufRead = null;
		int actualNum = 1;
		String res;

		try {
			fIn = leContext.openFileInput(fileName);
		} catch (FileNotFoundException e1) {
			throw e1;
		}       
		try {
			isr = new InputStreamReader(fIn); 
			bufRead = new BufferedReader(isr);
			while(actualNum < numLine) {
				bufRead.readLine();
				actualNum++;
			}
			res = bufRead.readLine();
			bufRead.close();
			isr.close();
			fIn.close();
			if (reading) {
				startReading();
			}
			return res;
		} catch (IOException e) {
			throw e;
		}
	}

	public void writeLine(String s) {
		if (writing) {
			try {
				writer.write(s + "\r\n");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			FileOutputStream fOut = null; 
			OutputStreamWriter osw = null; 

			try { 
				fOut = leContext.openFileOutput(fileName, Context.MODE_APPEND);
				osw = new OutputStreamWriter(fOut); 
				osw.write(s + "\r\n");

				osw.flush(); 
				osw.close(); 
				fOut.close();
			} catch (Exception e) {       
				//Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
			} 
		}
	}

	public void startWriting() {
		try {
			fileOut = leContext.openFileOutput(fileName, Context.MODE_APPEND);
			writer = new OutputStreamWriter(fileOut);
			writing = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void stopWriting() {
		try {
			writer.flush();
			writer.close();
			fileOut.close();
			writing = false;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void startReading() {
		try {
			fileIn = leContext.openFileInput(fileName);
			inputReader = new InputStreamReader(fileIn); 
			reader = new BufferedReader(inputReader);
			reading = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void stopReading() {
		try {
			reader.close();
			inputReader.close();
			fileIn.close();
			reading = false;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addLine(String s) {
		if (writing) {
			try {
				writer.write(s + "\r\n");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			FileOutputStream fOut = null; 
			OutputStreamWriter osw = null; 
			FileInputStream fIn = null; 
			InputStreamReader isr = null; 
			BufferedReader bufRead = null;

			try { 
				fIn = leContext.openFileInput(fileName);
				isr = new InputStreamReader(fIn); 
				bufRead = new BufferedReader(isr);

				fOut = leContext.openFileOutput(fileTemp, Context.MODE_APPEND);
				osw = new OutputStreamWriter(fOut); 

				String line = "";
				while((line = bufRead.readLine()) != null) {
					osw.write(line + "\r\n");
				}
				osw.write(s + "\r\n");

				bufRead.close();
				osw.flush(); 
				osw.close(); 
				fOut.close();
				leContext.getFileStreamPath(fileName).delete();
				leContext.getFileStreamPath(fileTemp).renameTo(leContext.getFileStreamPath(fileName));
			} catch (Exception e) {       
				//Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
			}  
		}
	}

	public void deleteFile() {
		try{ 
			leContext.getFileStreamPath(fileName).delete();
		} 
		catch (Exception e) {       
			Toast.makeText(leContext, "Pas Deletage",Toast.LENGTH_SHORT).show(); 
		} 

	}

	public void replaceLine(int numLine, String s) {
		FileOutputStream fOut = null; 
		OutputStreamWriter osw = null; 
		FileInputStream fIn = null; 
		InputStreamReader isr = null; 
		BufferedReader bufRead = null;

		try{ 
			fIn = leContext.openFileInput(fileName);
			isr = new InputStreamReader(fIn); 
			bufRead = new BufferedReader(isr);

			fOut = leContext.openFileOutput(fileTemp, Context.MODE_APPEND);
			osw = new OutputStreamWriter(fOut); 

			String line = "";
			int numActualLine = 0;

			while((line = bufRead.readLine()) != null && numActualLine < numLine - 1) {
				osw.write(line + "\r\n");
				numActualLine++;
			}
			osw.write(s + "\r\n");
			while((line = bufRead.readLine()) != null) {
				osw.write(line + "\r\n");
			}

			bufRead.close();
			osw.flush(); 
			osw.close(); 
			fOut.close();
			leContext.getFileStreamPath(fileName).delete();
			leContext.getFileStreamPath(fileTemp).renameTo(leContext.getFileStreamPath(fileName));
		} catch (Exception e) {       
			//Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
		} 
	}
}
